#ifndef _COMPAT_EVENTS_EPOLL_H_
#define _COMPAT_EVENTS_EPOLL_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <crt_util.h>
#include "compat_event_util.h"

#if (TARGET_OS == OS_POSIX)

typedef struct epoll_op_s epoll_op_t;
typedef struct epoll_event_s eevent_t;

struct epoll_event_s {
    int ctl;
    uint32_t op;
	
	evoper_t* oper;
	evoper_queue queue;
	evevent event;
};

typedef int (*epoll_create_fn)(int);
typedef int (*epoll_wait_fn)(int, struct epoll_event*, int, int);
typedef int (*epoll_ctl_fn)(int, int, int, struct epoll_event*);
typedef int (*sys_eventfd_fn)(unsigned int, int);

//#ifndef TFD_CLOEXEC
//#define TFD_CLOEXEC 02000000
//#endif // !TFD_CLOEXEC
//
//#ifndef TFD_NONBLOCK
//#define TFD_NONBLOCK 00004000
//#endif // !TFD_NONBLOCK
//
// typedef int (*timerfd_create_fn)(int, int);
// typedef int (*timerfd_settime_fn)(int, int, const struct itimerspec*, struct itimerspec*);
// typedef int (*timerfd_gettime_fn)(int, struct itimerspec*);

typedef int (*base_notify_handler)(epoll_op_t* base);

struct epoll_op_s {
    int epfd;

    struct epoll_event* events;
    struct epoll_event* poll;
    int size;
    epoll_create_fn sys_epoll_create;
    epoll_wait_fn sys_epoll_wait;
    epoll_ctl_fn sys_epoll_ctl;
    sys_eventfd_fn sys_eventfd;

    // notify->epoll 2.6.22
    int notify_fd;
    eevent_t notify_event;
    base_notify_handler notify_handler;

    // op
    ev_op ev;

    // mode
    uint32_t mode;
	
	_mutex_t lock;
	evloop_queue_t queue;
};

int epoll_evop_t(evsysop_t* op);
int epollevop_set_oper(eevent_t* ptr, evoper_t* oper);

#endif

#ifdef __cplusplus
}
#endif

#endif
